<?php

namespace Modules\Asset\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Asset\Entities\PackageSku;

use Validator;
use ThrowException;
use Auth;
use Setting;
use DB;

// 已开出物品
class ReturnSaleController extends Controller
{
    public function preview(Request $req)
    {
        $rule = [
            'ids' => ['required', 'array'],  // packageSku的id数组
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        $ids = $req->input('ids');

        // 所有要返售的sku
        $packageSkus = PackageSku::belongUser($user)->returnSaleable()->whereIn('id', $ids)->get();
        
        $skus = [];
        $only = [
            'title',
            'number',
            'thumb',
            'money_price',
            'score_price',
            'total'
        ];

        $returnRatio = Setting::returnSaleRatio();
        foreach ($packageSkus as $sku) {
            $skuInfo = $sku->getInfo($only);
            if($sku->node_type=='egg_lottery'){
        
                $skuInfo['money_price'] = $sku->node_money_price;
            }
            list($returnMoney, $returnRedpack, $returnScore) = $sku->calcReturnSaleInfo();

            if($sku->node_type=='choushang'){
                $returnSaleRatio = DB::select("select return_sale_ratio from choushang_skus where activity_id = {$sku->node_id} and id = {$sku->sku_id} and title = '".addslashes($skuInfo['title'])."' ");
                if ( count($returnSaleRatio) != 0 ){
                    $returnSaleRatio = object_to_array($returnSaleRatio);
                    if ( isset($returnSaleRatio[0]['return_sale_ratio']) && is_numeric($returnSaleRatio[0]['return_sale_ratio']) ){
                        $returnRatio = $returnSaleRatio[0]['return_sale_ratio']?:0;
                    }
                }else{
                    $returnSaleRatio = DB::select("select return_sale_ratio from choushang_last_skus where activity_id = {$sku->node_id} and id = {$sku->sku_id} and title = '".addslashes($skuInfo['title'])."' ");
                    if ( count($returnSaleRatio) != 0 ){
                        $returnSaleRatio = object_to_array($returnSaleRatio);
                        if ( isset($returnSaleRatio[0]['return_sale_ratio']) && is_numeric($returnSaleRatio[0]['return_sale_ratio']) ){
                            $returnRatio = $returnSaleRatio[0]['return_sale_ratio']?:0;
                        }
                    }
                }
            }

            if($sku->node_type=='leitai'){
                $returnSaleRatio = DB::select("select return_sale_ratio from leitai_skus where activity_id = {$sku->node_id} and id = {$sku->sku_id} and title = '".addslashes($skuInfo['title'])."' ");
                if ( count($returnSaleRatio) != 0 ){
                    $returnSaleRatio = object_to_array($returnSaleRatio);
                    if ( isset($returnSaleRatio[0]['return_sale_ratio']) && is_numeric($returnSaleRatio[0]['return_sale_ratio']) ){
                        $returnRatio = $returnSaleRatio[0]['return_sale_ratio']?:0;
                    }
                }else{
                    $returnSaleRatio = DB::select("select return_sale_ratio from leitai_last_skus where activity_id = {$sku->node_id} and id = {$sku->sku_id} and title = '".addslashes($skuInfo['title'])."' ");
                    if ( count($returnSaleRatio) != 0 ){
                        $returnSaleRatio = object_to_array($returnSaleRatio);
                        if ( isset($returnSaleRatio[0]['return_sale_ratio']) && is_numeric($returnSaleRatio[0]['return_sale_ratio']) ){
                            $returnRatio = $returnSaleRatio[0]['return_sale_ratio']?:0;
                        }
                    }
                }
            }

            $skuInfo['return_money'] = $returnMoney;
            $skuInfo['return_redpack'] = $returnRedpack;
            $skuInfo['return_score'] = $returnScore;

            array_push($skus, $skuInfo);
        }

        list($returnMoney, $returnRedpack, $returnScore) = PackageSku::calcReturnSaleInfoForList($packageSkus);

        $info = [
            'skus' => $skus,
            'return_score' => $returnScore,
            'return_money' => $returnMoney,
            'return_redpack' => $returnRedpack,
            'return_sale_ratio' => $returnRatio
        ];

        return $info;
    }

    public function confirm(Request $req)
    {
        $rule = [
            'ids' => ['required', 'array'],  // packageSku的id数组
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        $ids = $req->input('ids');

        // 所有要返售的sku
        $packageSkus = PackageSku::belongUser($user)->returnSaleable()->whereIn('id', $ids)->get();

        // 无可转售商品
        if ($packageSkus->count() === 0) {
            ThrowException::Conflict('当前无可转售商品~');
        }

        list($returnMoney, $returnRedpack, $returnScore) = PackageSku::calcReturnSaleInfoForList($packageSkus);

        foreach ($packageSkus as $sku) {
            DB::beginTransaction();

            // 单个sku返售
            $sku->returnSale();

            DB::commit();
        }

        return [];
    }
}
